﻿<%@ WebHandler Language="C#" Class="CommunityHandler" %>

using System;
using System.Web;
using Dln.Handler;
using Dln.Common.Helper;
using Dln.Model;
using Dln.Common;
using System.Collections.Generic;
using System.Linq;

public class CommunityHandler : HandlerBase
{

    public string url = CommonHelper.GetAppSettings("DlnService1");
    /// <summary>
    /// 
    /// </summary>
    public void AddUserInfo()
    {
        //类型
        int type = Convert.ToInt32(_httpReuqestString["type"]);

        //医生
        string workID = _httpRequestForm["WorkID"].ToString();
        string password = _httpRequestForm["password"].ToString();

        //患者
        string identity = _httpRequestForm["identity"].ToString();
        string patientID = _httpRequestForm["PatientID"].ToString();
        string openid = _httpReuqestString["openid"].ToString();

        if (type == 0)
        {
            object[] args = new object[2];
            args[0] = (workID);
            args[1] = (password);
            object result = WebServiceHelper.InvokeWebService(url, "Onlogin", args);
            if (result == null || (string)result == "False")
            {
                PrintErrorJson("未查到该医生信息，请确认账号密码！");
                return;
            }
        }


        var uptModel = SqlHelper.Context.From<WeChatUsers>().Where(d => d.Openid == openid);
        if (uptModel != null && uptModel.Count() > 0)
        {
            WeChatUsers model = uptModel.ToFirstDefault();
            model.CardID = (type == 0 ? workID : identity);
            model.PatientID = (type == 0 ? "" : patientID);
            model.RoleType = type;
            var count = SqlHelper.Context.Update<WeChatUsers>(model);
            if (count > 0)
            {
                PrintSuccessJson("保存成功！");
            }
            else
            {
                PrintErrorJson("保存失败！");
            }
        }
        else
        {
            PrintErrorJson("未查到关注信息，请重新关注！");
        }
    }

    /// <summary>
    /// 保存话题
    /// </summary>
    public void SaveTopic()
    {
        string TopicContent = _httpRequestForm["TopicContent"].ToString();
        string Pic = _httpRequestForm["Pic"].ToString();
        string openid = _httpRequestForm["openid"].ToString();
        string cardId = _httpRequestForm["CardId"].ToString();

        var userModel = SqlHelper.Context.From<WeChatUsers>().Where(d => d.Openid == openid);
        if (userModel != null && userModel.Count() > 0)
        {
            var communityTopicModel = new Community_Topic();
            communityTopicModel.AddTime = DateTime.Now;
            communityTopicModel.openid = openid;
            communityTopicModel.Contents = TopicContent;
            communityTopicModel.imgs = Pic.Trim(',');
            communityTopicModel.Name = userModel.ToFirstDefault().Nickname;
            communityTopicModel.IsRead = 0;
            communityTopicModel.HeadImg = userModel.ToFirstDefault().Headimgurl;
            communityTopicModel.CardId = cardId;
            var count = SqlHelper.Context.Insert<Community_Topic>(communityTopicModel);
            if (count > 0)
            {
                PrintSuccessJson("保存成功！");
            }
            else
            {
                PrintErrorJson("保存失败！");
            }
        }
        else
        {

            PrintErrorJson("查到该患者信息！");
        }
    }

    /// <summary>
    /// 新增回复
    /// </summary>
    public void AddComment()
    {
        string content = _httpRequestForm["content"].ToString();
        string openid = _httpRequestForm["openid"].ToString();
        int topicId = Convert.ToInt32(_httpRequestForm["topicId"].ToString());


        var userModel = SqlHelper.Context.From<WeChatUsers>().Where(d => d.Openid == openid);
        if (userModel != null && userModel.Count() > 0)
        {
            var Model = new Community_TopicReply();
            Model.AddTime = DateTime.Now;
            Model.Contents = content;
            Model.ReplyOpenid = openid;
            Model.ReplyName = userModel.ToFirstDefault().Nickname;
            Model.ReplyId = topicId;
            Model.IsRead = 0;
            Model.ReplyHeadImg = userModel.ToFirstDefault().Headimgurl;

            var count = SqlHelper.Context.Insert<Community_TopicReply>(Model);
            if (count > 0)
            {
                PrintSuccessJson("回复成功！");
            }
            else
            {
                PrintErrorJson("回复失败！");
            }

        }
        else
        {

            PrintErrorJson("查到该患者信息！");
        }
    }

    /// <summary>
    /// 关闭咨询
    /// </summary>
    public void CloseTopic()
    {
        int topicId = Convert.ToInt32(_httpRequestForm["TopicId"].ToString());
        string flag = _httpRequestForm["Flag"].ToString();

        Community_Topic model = new Community_Topic();
        model.IsClosed = flag == "true" ? 1 : 0;
        var count = SqlHelper.Context.Update<Community_Topic>(model, d => d.Id == topicId);
    }


    /// <summary>
    /// 补全报修人员信息
    /// </summary>
    public void AddWorkInfo()
    {
        string workID = _httpRequestForm["WorkID"].ToString();
        string password = _httpRequestForm["password"].ToString();
        string openid = _httpReuqestString["openid"].ToString();
        string md5Password = SecurityHelper.Md5EncryptStr32(password);
        if (string.IsNullOrEmpty(openid)) PrintErrorJson("未查到Openid,请联系信息科！");


        var userModel = SqlHelper.ContextDlnOffice.From<Users>().Where(d => d.UserId == workID && d.Password == md5Password).First();
        if (userModel != null)
        {
            var uptModel = SqlHelper.Context.From<WeChatUsers>().Where(d => d.Openid == openid).ToFirst();
            if (uptModel != null)
            {
                uptModel.CardID = userModel.UserId;
                var count = SqlHelper.Context.Update<WeChatUsers>(uptModel);
                if (count > 0)
                {
                    PrintSuccessJson("保存成功！");
                }
                else
                {
                    PrintErrorJson("保存失败！");
                }
            }
            else
            {
                PrintErrorJson("未查到关注信息，请重新关注！");
            }
        }
        else
        {
            PrintErrorJson("工号或密码错误");
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public void GetQuestionList()
    {
        int PageIndex = Convert.ToInt32(_httpRequestForm["PageIndex"].ToString());

        string openid = _httpRequestForm["Openid"];
        int PageSize =Convert.ToInt32(_httpRequestForm["PageSize"].ToString());
        IList<Community_Topic> CommunityTopicList = SqlHelper.Context.From<Community_Topic>().Where(d => d.openid == openid && d.DelStatus == 0).ToList();
        if (CommunityTopicList != null)
        {
            foreach (var item in CommunityTopicList)
            {
                var replyModel = SqlHelper.Context.From<Community_TopicReply>().Where(d => d.ReplyId == item.Id && d.Delstatus == 0 && d.IsRead == 0);
                if (replyModel != null && replyModel.Count() > 0)
                {
                    item.Number = replyModel.Count();
                }
            }
            CommunityTopicList = CommunityTopicList.OrderByDescending(d => d.Number).Skip(PageSize * (PageIndex - 1)).Take(PageSize).ToList();
        }


        HttpContext.Current.Response.Write(JSONHelper.ObjectToJson(CommunityTopicList));
        HttpContext.Current.Response.End();
    }
}